import { createRouter, createWebHistory } from "vue-router";




const router=createRouter({
    history:createWebHistory(), //选择hsah路由
    routes:[
        {
            path:'/',
            redirect:'/login'
        },
        {
            path:'/home',
            component:()=>import('../views/Home.vue'),
            meta:{
                isAuth:true //需要登录才能访问
            },
        },
        
        {
            path:'/login',
            component:()=>import('../views/Login.vue')
        },


        {
            path:'/register',
            component:()=>import('../views/Register.vue')
        },

    ],
});



//路由守卫
router.beforeEach((to,from,next)=>{
    if(to.meta.isAuth){
        if(localStorage.getItem('token')){ //判断是否登录
            next()
        }else{
            ElMessage.error('请先登录')
        }
    }else{
        next()
    }
})

export default router;